package com.hua.yuan.http;

import android.util.Log;

import com.zhy.http.okhttp.OkHttpUtils;

import java.io.IOException;
import java.util.Map;

import okhttp3.Response;

/**
 * Created by hy on 2017/3/4.
 * 网络同步请求
 */

public class RequestUtil {

    /**
     * 同步的http请求
     *
     * @param tag    标记
     * @param url    action
     * @param params 参数
     * @return Response 可能为null
     */
    public static Response post(String tag, String url, Map<String, String> params) {


        if (url == null) {

            Log.i(tag, "action is null or length is 0");

            return null;
        }
        if (url.length() == 0) {

            Log.i(tag, "url is null or length is 0");

            return null;
        }


        String _url = BaseUrl.getBaseSslUrl() + url + "?";

        Response response = null;
        try {
            if (params != null){
                response = OkHttpUtils.post()
                        .url(_url)
                        .params(params)
                        .build().execute();
            }else {
                response = OkHttpUtils.post()
                        .url(_url)
                        .build().execute();
            }


            Log.i(tag, "请求成功 = " + response.code());


        } catch (IOException e) {
            Log.i(tag, "请求失败 " + e.getMessage());
        }

        if (response != null && response.isSuccessful()) {
            return response;
        } else {
            return null;
        }

    }

    /**
     * 同步的https请求
     *
     * @param tag    标记
     * @param url    action
     * @param params 参数
     * @return Response 可能为null
     */
    public static Response postSSL(String tag, String url, Map<String, String> params) {


        if (url == null) {

            Log.i(tag, "action is null or length is 0");

            return null;
        }
        if (url.length() == 0) {

            Log.i(tag, "action is null or length is 0");

            return null;
        }

        String _url = BaseUrl.getBaseSslUrl() + url + "?";

        Response response = null;
        try {
            response = OkHttpUtils.post()
                    .url(_url)
                    .params(params)
                    .build().execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 同步的http请求
     *
     * @param tag    标记
     * @param url    action
     * @param params 参数
     * @return Response 可能为null
     */
    public static Response get(String tag, String url, Map<String, String> params) {



        if (url == null) {

            Log.i(tag, "action is null or length is 0");

            return null;
        }
        if (url.length() == 0) {

            Log.i(tag, "url is null or length is 0");

            return null;
        }


        String _url = BaseUrl.getBaseUrl() + url + "?";

        Response response = null;
        try {
            if (params == null) {
                response = OkHttpUtils.get()
                        .url(_url)
                        .build().execute();
            } else {
                response = OkHttpUtils.get()
                        .url(_url)
                        .params(params)
                        .build().execute();
            }


            Log.i(tag, "请求成功 = " + response.code());


        } catch (IOException e) {
            Log.i(tag, "请求失败 " + e.getMessage());
        }

        if (response != null && response.isSuccessful()) {
            return response;
        } else {
            return null;
        }

    }

}
